#!/usr/bin/env python
import pcf8591 as ADC
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

def setup():
    ADC.setup(0x48)

def loop():
    count = 0
    while True:
        voiceValue = ADC.read(0)  #若检测到有声音，该值会变小
        if voiceValue:
            print('Current voice value=%d' % voiceValue)
            if voiceValue < 100:  
                #我实验时安静状态下的值约为133，所以设置检测到有声音的阈值设为130
                print("The %d th voice detected, voice value=%d! " % (count, voiceValue))
                count += 1
            time.sleep(0.3)

if __name__ == '__main__':
    try:
        setup()
        loop()
    except KeyboardInterrupt: 
        pass    


